package com.example.seata.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.seata.account.entity.AccountTbl;
import com.example.seata.account.mapper.AccountTblMapper;
import com.example.seata.account.service.IAccountTblService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.example.enums.CommonErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author tianshuo
 * @since 2021-05-15
 */
@Service
public class AccountTblServiceImpl extends ServiceImpl<AccountTblMapper, AccountTbl> implements IAccountTblService {


    @Autowired
    private AccountTblMapper accountTblMapper;

    /**
     * 扣款
     *
     * @param accountTbl
     * @return
     */
    @Transactional
    @Override
    public boolean debit(AccountTbl accountTbl) {
        QueryWrapper<AccountTbl> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AccountTbl::getUserId, accountTbl.getUserId());
        AccountTbl accountTbl1 = getOne(queryWrapper);
        if (accountTbl.getMoney() > accountTbl1.getMoney()) {
            throw new RuntimeException(CommonErrorCode.BIZ_ERROR.getDesc());
        }
        accountTblMapper.updateMoney(accountTbl);
        return true;
    }

}
